﻿using ArithCollect;
using Slex;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Test.Scenes {
    /// <summary>
    /// 简单的雪花处理 营造一个夜色中下雪的场景
    /// </summary>
    /// <remark>
    /// 500个 Ellipse对象
    /// 故事板需要关注的属性包括：
    /// RanderTransform.Y, Opacity, Size
    /// 要求 每次按一定的 起始位置生成 雪花，但每次刷新后 雪花位置应该是刷新过后的(高阶要求）
    /// </remark>
    public partial class SnowNight : UserControl {
        public SnowNight() {
            InitialUserData();
            InitializeComponent();
            InitialUserCom();
            InitialUserSetting();
        }
        public void InitialUserData() {
        }
        Random ran = new Random();
        public void InitialUserCom() {
        }
        public void InitialUserSetting() {
            this.Loaded += Page_Loaded;
            this.Unloaded += Page_Unloaded;
        }
        void Page_Unloaded(object sender, RoutedEventArgs e) { }
        void Page_Loaded(object sender, RoutedEventArgs e2) {
            var count = 300;
            for (int i = 0; i < count; i++) {
                var e = new Ellipse();
                e.Fill = new SolidColorBrush(Colors.Transparent);
                e.Width = e.Height = ran.Next(1, 7);
                var dm1 = new DoubleAnimation();
                Storyboard.SetTarget(dm1, e);
                Storyboard.SetTargetProperty(dm1, new PropertyPath("Ellipse.Opacity"));
                dm1.From = ran.NextDouble() + 0.8;
                dm1.To = 0.3;
                dm1.Duration = TimeSpan.FromSeconds(5);
                dm1.RepeatBehavior = new RepeatBehavior(100);

                var dm3 = new ColorAnimation();
                Storyboard.SetTarget(dm3, e);
                Storyboard.SetTargetProperty(dm3, new PropertyPath("(Ellipse.Fill).(SolidColorBrush.Color)"));
                //dm3.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.0)), Value = Brushs.Transparent.ToBrush() });
                //dm3.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)), Value = Brushs.Snow.ToBrush() });
                //dm3.From = Colors.Transparent;
                dm3.By = Brushs.Snow.ToColor();
                dm3.Duration = TimeSpan.FromMilliseconds(ran.Next(1000));
                dm3.BeginTime = TimeSpan.FromMilliseconds(ran.Next(8000));
                //dm3.RepeatBehavior = new RepeatBehavior(100);

                e.RenderTransform = new TranslateTransform();
                var dm2 = new DoubleAnimation();
                Storyboard.SetTarget(dm2, e);
                Storyboard.SetTargetProperty(dm2, new PropertyPath("(Ellipse.RenderTransform).(TranslateTransform.Y)"));
                dm2.Duration = TimeSpan.FromSeconds(5);
                dm2.From = -100;
                dm2.To = 450 + ran.Next(50);
                dm1.BeginTime = TimeSpan.FromMilliseconds(ran.Next(0, 4999));
                dm2.BeginTime = TimeSpan.FromMilliseconds(ran.Next(0, 4999));
                dm2.RepeatBehavior = new RepeatBehavior(100);
                e.Effect = new System.Windows.Media.Effects.BlurEffect();
                //
                sb.Children.Add(dm1);
                sb.Children.Add(dm2);
                sb.Children.Add(dm3);
                this.LayoutRoot.Children.Add(e);
                Canvas.SetTop(e, ran.Next(50));
                Canvas.SetLeft(e, ran.Next(50, (int)(this.LayoutRoot.ActualWidth - 50)));
            }
            sb.Begin();
        }
    }
}
